<template>
  <view class="container">
    <view class="c-p-header color flex justify-center align-center">
      <view class="text-17"> ¥ </view>
      <view class="text-23">
        {{ parseFloat(realAmount / 100).toFixed(2) }}
      </view>
    </view>
    <view class="margin-sm padding bg-white radius10">
      <view class="text-15 text-333"> 支付详情 </view>
      <view class="flex justify-between align-center text-13 margin-top">
        <view class="text-999"> 应付金额 </view>
        <view class="color">
          ¥{{ parseFloat(realAmount / 100).toFixed(2) }}
        </view>
      </view>
      <view class="flex justify-between align-center text-13 margin-top">
        <view class="text-999"> 优惠券 </view>
        <view class="text-333">
          ¥{{ parseFloat(promoteAmount / 100).toFixed(2) }}
        </view>
      </view>
      <view class="flex justify-between align-center text-13 margin-top">
        <view class="text-999"> 支付方式 </view>
        <view class="text-333">
          {{ payType == "A" ? "微信支付" : "账户余额支付" }}
        </view>
      </view>
    </view>
    <passkeyborad
      :money="money"
      @message="message"
      @close="close"
      :show="show"
    ></passkeyborad>
    <button
      @click="submit"
      class="bg-to-right round padding text-white text-15"
    >
      立即支付
    </button>
  </view>
</template>

<script>
import passkeyborad from "@/components/yzc-paykeyboard/yzc-paykeyboard.vue";
export default {
  data() {
    return {
      orderId: "",
      payType: "",
      realAmount: "",
      promoteAmount: "",
      show: false,
      password: 0,
      money: 0,
      serviceType: "",
      type: "",
      groupStatus: "",
    };
  },
  components: {
    passkeyborad,
  },
  onLoad(options) {
    if (options.groupStatus) this.groupStatus = options.groupStatus;
    if (options.orderId) this.orderId = options.orderId;
    if (options.payType) this.payType = options.payType;
    if (options.realAmount) this.realAmount = options.realAmount;
    if (options.promoteAmount) this.promoteAmount = options.promoteAmount;
    if (options.type) this.type = options.type;
  },
  onUnload() {
    uni.setStorageSync("isBack", true);
    uni.switchTab({
      url: "/pages/home/home",
    });
  },
  methods: {
    async submit() {
      let that = this;
      // #ifdef  MP-WEIXIN
      const data = {
        orderId: this.orderId,
        payWay: 1,
        appId: this.$config.wxAppId,
        openId: uni.getStorageSync("openid"),
      };
      // #endif

      // #ifdef  APP-PLUS
      const data = {
        orderId: this.orderId,
        payWay: 2,
        appId: this.$config.appId,
      };
      // #endif
      uni.showLoading({
        title: "正在支付",
        mask: true,
      });
      console.log(data);
      let url;
      if (this.type == 1) {
        url = this.$api.order_wxPayList;
      } else {
        url = this.$api.order_wxPay;
      }
      if (this.payType == "A") {
        const [error, rs] = await this.$post(url, data);
        if ((error, rs)) {
          console.log(rs);
          uni.hideLoading();
          const data = rs.data;
          // #ifdef  MP-WEIXIN
          uni.requestPayment({
            provider: "wxpay",
            appId: data.appId,
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data._package,
            signType: data.signType,
            paySign: data.paySign,
            success: function (res) {
              console.log("success:" + JSON.stringify(res));

              setTimeout(() => {
                if (that.groupStatus) {
                  uni.navigateTo({
                    url: "/filePage/pages/group-order/group-order",
                  });
                } else {
                  uni.navigateTo({
                    url: "/filePage/pages/mall-order/mall-order?title=商城订单",
                  });
                }
              }, 1500);
            },
            fail: function (err) {
              console.log("fail:" + JSON.stringify(err));
            },
          });
          // #endif

          // #ifdef  APP-PLUS
          uni.requestPayment({
            provider: "wxpay",
            orderInfo: {
              appid: data.appId, // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
              noncestr: data.nonceStr, // 随机字符串
              package: data._package, // 固定值
              partnerid: data.partnerid, // 微信支付商户号
              prepayid: data.prepayid, // 统一下单订单号
              timestamp: data.timeStamp, // 时间戳（单位：秒）
              sign: data.sign, // 签名，这里用的 MD5 签名
            },
            success(res) {
              setTimeout(() => {
                // uni.navigateTo({
                //   url: "/filePage/pages/mall-order/mall-order?title=商城订单",
                // });
				if (that.groupStatus) {
				  uni.navigateTo({
				    url: "/filePage/pages/group-order/group-order",
				  });
				} else {
				  uni.navigateTo({
				    url: "/filePage/pages/mall-order/mall-order?title=商城订单",
				  });
				}
              }, 1500);
            },
            fail(e) {},
          });
          // #endif
        }
      } else {
        this.show = true;
        this.money = this.realAmount;
        uni.hideLoading();

        // const [error, res] = await this.$post(this.$api.order_accountPay, data);
        // if (res) {
        // 	// console.log(res);
        // 	this.$util.msg(res.message)
        // 	uni.hideLoading();
        // 	setTimeout(() => {
        // 		uni.navigateTo({
        // 			url:'/filePage/pages/mall-order/mall-order?title=商城订单'
        // 		})
        // 	}, 1500)
        // }
      }
    },
    close() {
      // console.log(111111)
      this.show = false;
    },
    message(password) {
      console.log(password);
	  let that = this
      var CusBase64 = require("@/common/js/base64.js");
      //函数调用
      var passWord = CusBase64.CusBASE64.encoder(password);
      let url;
      if (this.type == 1) {
        url = this.$api.order_accountPayList;
      } else {
        url = this.$api.order_accountPay;
      }
      this.$get(this.$api.verifyPayPassWord, {
        passWord,
        userId: uni.getStorageSync("userId"),
      }).then((res) => {
        const [err, rs] = res;
        if (rs) {
          const data = {
            orderId: this.orderId,
          };
          this.$post(url, data).then((result) => {
            const [err, rs] = result;
			if(err){
			  this.show = false;
			}
			
			
            if (rs) {
				console.log(rs)
				
              this.$util.msg(rs.message);
              this.show = false;
              setTimeout(() => {
				  if (that.groupStatus) {
				    uni.navigateTo({
				      url: "/filePage/pages/group-order/group-order",
				    });
				  } else {
				    uni.navigateTo({
				      url: "/filePage/pages/mall-order/mall-order?title=商城订单",
				    });
				  }
                // uni.navigateTo({
                //   url: "/filePage/pages/mall-order/mall-order?title=商城订单",
                // });
              }, 1500);
            }
			
          });
		  
        }
        if (err) {
          this.show = false;
          if (err.message == "请去设置密码！") {
            setTimeout(() => {
              uni.navigateTo({
                url: "/pagesA/pages/setPassword/setPassword",
              });
            }, 1500);
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.c-p-header {
  height: 156rpx;
}

button {
  margin: 128rpx 20rpx;
}
</style>
